package Controller;

import DAOs.AdministrativeAssistantJpaController;
import DAOs.AdministratorJpaController;
import DAOs.VivariumOperatorJpaController;
import Entity.AdministrativeAssistant;
import Entity.Administrator;
import Entity.User;
import Entity.VivariumOperator;
import java.util.Iterator;
import java.util.List;
import javax.persistence.EntityManagerFactory;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author USUARIO
 */
public class LoginController {
    
    private AdministratorJpaController administratorJpaController;
    private AdministrativeAssistantJpaController administrativeAssistantsJpaController;
    private VivariumOperatorJpaController vivariumOperatorsJpaController;
    private String notMatchMessage = "Combinación de usuario y contraseña incorrectos";
    private String emptyMessage = "Por favor ingrese usuario y cotraseña";
    private int ADMIN = 0;
    private int ASSISTANT = 1;
    private int ERROR_CODE = 5;

    public LoginController(EntityManagerFactory emf) {
        administratorJpaController = new AdministratorJpaController(emf);
        administrativeAssistantsJpaController = new AdministrativeAssistantJpaController(emf);
        vivariumOperatorsJpaController = new VivariumOperatorJpaController(emf);
}

    public int validateLogin(String username, String password) {

        if (!password.isEmpty() && !username.isEmpty()) {
            User usuario = verifyLogin(username, password);
            if (usuario == null) {
                JOptionPane.showMessageDialog(new JFrame(), notMatchMessage, "Error", JOptionPane.ERROR_MESSAGE);
            } else {
                if (usuario.getClass() == Administrator.class) {
                    JOptionPane.showMessageDialog(new JFrame(), "Bienvenido " + usuario.getName(), "Dialog", JOptionPane.INFORMATION_MESSAGE);
                    return ADMIN;
                } else {
                    if (usuario.getClass() == AdministrativeAssistant.class) {
                        JOptionPane.showMessageDialog(new JFrame(), "Bienvenido " + usuario.getName(), "Dialog", JOptionPane.INFORMATION_MESSAGE);
                        return ASSISTANT;
                    }
                }
            }
        } else {
            JOptionPane.showMessageDialog(new JFrame(), emptyMessage, "Error", JOptionPane.ERROR_MESSAGE);
            return ERROR_CODE;
        }
        return ERROR_CODE;
    }

    public User verifyLogin(String username, String password) {
        List<Administrator> administrators = administratorJpaController.findAdministratorEntities();
        List<AdministrativeAssistant> assistants = administrativeAssistantsJpaController.findAdministrativeAssistantEntities();
        List<VivariumOperator> operators = vivariumOperatorsJpaController.findVivariumOperatorEntities();

        Iterator itr = administrators.iterator();
        while (itr.hasNext()) {
            Administrator currentAdministrator = (Administrator) itr.next();
            if (validateCurrent(username, password, currentAdministrator)) {
                return currentAdministrator;
            }
        }
        itr = assistants.iterator();
        while (itr.hasNext()) {
            AdministrativeAssistant currentAssistant = (AdministrativeAssistant) itr.next();
            if (validateCurrent(username, password, currentAssistant)) {
                return currentAssistant;
            }
        }
        return null;
    }

    private boolean validateCurrent(String username, String password, User user) {
        boolean passwordCorrect = BCrypt.checkpw(password, user.getUserPassword());
        boolean usernameCorrect = username.equals(user.getUserName());
        if ((usernameCorrect) && (passwordCorrect)) {
            return true;
        } else {
            return false;
        }
    }
}
